<%@page import="dao.MngClass"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@page import="beans.Account"%>
<%@page import="java.util.List"%>
<%@page import="dao.CheckAccount"%>

<html>
    <head>
        <script language="javascript">
            function delete1()
            {
                return confirm('Do you want delete this record');
            }
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Show Account Page</title>
        <style>                        
            table, th, td {
                border: 1px solid #619702;
                border-collapse: collapse;
                width:99.3% ;
            }
            th, td {
                padding: 5px;
                width: 100px;
            }
            th {
                text-align: center;
            }
        </style>
    </head>
    <body>     
        <%
            HttpSession ss_list_acc = request.getSession();
            String roll_list_acc = ss_list_acc.getAttribute("roll").toString();
            if (request.getAttribute("msg") != null) {
        %>
        <font color="green" ><%= request.getAttribute("msg")%></font>
        <%
            }
            if (request.getAttribute("error") != null) {
        %>
        <font color="red" > <%= request.getAttribute("error")%> </font>
        <%
            }
            if (roll_list_acc.equals("admin")) {
        %>
        <br><a href="index.jsp?page=createuser">Create new account</a>  
        <br>
        <%
            }
            if (request.getAttribute("class_id") == null && roll_list_acc.equals("admin")) {
        %>
        <form action="editAcc" method="get">
            <input type="hidden" name="action" value="show_by_roll">            
            <select name="roll">
                <option value="">All</option>
                <option value="student">Student</option>
                <option value="staff">Staff</option>
                <option value="admin">Admin</option>
            </select>
            <input type="submit" value="Show" >
        </form>    
        <%
            }
        %>
        <form action="searchAcc">
            Search by <select name="bysearch">
                <option value="username">User Name</option>
                <option value="full_name">Full Name</option>
                <option value="email">Email</option>
                <option value="address">Address</option>
            </select>  Like 
            <input type="text" name="search" value="">
            <input type="submit" value="Search">
            <%if (request.getAttribute("class_id") != null && !request.getAttribute("class_id").equals("")) {
            %>
            <input type="hidden" name="class_id" value="<%= request.getAttribute("class_id")%>">
            <%
                }
            %>
        </form>
        <%
            CheckAccount ck = new CheckAccount();
            List<Account> listAcc = null;

            if (request.getAttribute("class_id") == null) {
                if (request.getAttribute("roll") == null || request.getAttribute("roll").equals("")) {
                    if (request.getAttribute("bysearch") == null || request.getAttribute("bysearch").equals("")) {
                        listAcc = ck.showAccount();
                    } else {
                        if (request.getAttribute("bysearch").equals("username")) {
                            listAcc = ck.searchAccByUserName(request.getAttribute("search").toString());
                        } else if (request.getAttribute("bysearch").equals("full_name")) {
                            listAcc = ck.searchAccByFullName(request.getAttribute("search").toString());
                        } else if (request.getAttribute("bysearch").equals("email")) {
                            listAcc = ck.searchAccByEmail(request.getAttribute("search").toString());
                        } else if (request.getAttribute("bysearch").equals("address")) {
                            listAcc = ck.searchAccByAddress(request.getAttribute("search").toString());
                        }
                    }
                } else {
                    listAcc = ck.listAccByRoll(request.getAttribute("roll").toString());
                }
            } else {
                if (request.getAttribute("bysearch") == null || request.getAttribute("bysearch").equals("")) {
                    listAcc = ck.listAccByClassId(request.getAttribute("class_id").toString());
                } else {
                    if (request.getAttribute("bysearch").equals("username")) {
                        listAcc = ck.searchAccByUserNameAndClass(request.getAttribute("search").toString(), request.getAttribute("class_id").toString());
                    } else if (request.getAttribute("bysearch").equals("full_name")) {
                        listAcc = ck.searchAccByFullNameAndClass(request.getAttribute("search").toString(), request.getAttribute("class_id").toString());
                    } else if (request.getAttribute("bysearch").equals("email")) {
                        listAcc = ck.searchAccByEmailAndClass(request.getAttribute("search").toString(), request.getAttribute("class_id").toString());
                    } else if (request.getAttribute("bysearch").equals("address")) {
                        listAcc = ck.searchAccByAddressAndClass(request.getAttribute("search").toString(), request.getAttribute("class_id").toString());
                    }
                }
            }

            if (roll_list_acc.equals(
                    "staff")) {
        %>
        <form action="toAddStudent" method ="get">
            <input type="hidden" name="class_id" value="<%= request.getAttribute("class_id")%>">
            <input type="submit" value="Add Student">
        </form> <br>
        <%
            }
        %>
        <br>       
        <table>                
            <tr>
                <th>UserName</th> 
                <th>FullName</th>
                <th>Gender</th>
                <th>Address</th>
                <th>Phone</th>
                <th>Email</th>
                <th>Roll</th>   
                    <%
                        if (roll_list_acc.equals(
                                "admin")) {
                    %>                
                <th>Edit</th>
                <th>Delete</th>
                    <%
                    } else {%>
                <th>Delete Student</th>
                    <% } %>
            </tr> 
            <%
                String gender = "";
                if (listAcc
                        != null && (listAcc.size()
                        > 0)) {
                    for (Account acc : listAcc) {
                        if (acc.getAddress() == null) {
                            acc.setAddress("");
                        }
                        if (acc.getEmail() == null) {
                            acc.setEmail("");
                        }
                        if (acc.getPhone() == null) {
                            acc.setPhone("");
                        }
                        if (acc.getGender() == null) {
                            acc.setGender("");
                        }
            %>
            <tr>
                <th><%= acc.getUser_name()%></th>
                <th><%= acc.getFull_name()%></th>
                <th><% gender = acc.getGender();
                    if (gender.equals("0")) {
                        gender = "nữ";
                    } else {
                        gender = "nam";
                    }
                    %>
                    <%= gender%></th>
                <th><%= acc.getAddress()%></th>
                <th><%= acc.getPhone()%></th>
                <th><%= acc.getEmail()%></th>
                <th><%= acc.getRoll()%></th>                  
                    <%
                        if (roll_list_acc.equals("admin")) {
                    %>                
                <th>
            <form action="editAcc" method ="get">
                <input type="hidden" name="action" value="update">
                <input type="hidden" name="acc_id" value="<%= acc.getAcc_id()%>">
                <input type="hidden" name="username" value="<%= acc.getUser_name()%>">
                <input type="hidden" name="password" value="<%= acc.getPass_word()%>">
                <input type="hidden" name="full_name" value="<%= acc.getFull_name()%>">
                <input type="hidden" name="gender" value="<%= acc.getGender()%>">
                <input type="hidden" name="address" value="<%= acc.getAddress()%>">
                <input type="hidden" name="phone" value="<%= acc.getPhone()%>">
                <input type="hidden" name="email" value="<%= acc.getEmail()%>">
                <input type="hidden" name="roll" value="<%= acc.getRoll()%>">                
                <input type="submit" value="Update">
            </form>
        </th>
        <th>
        <form action="editAcc" method ="get">
            <input type="hidden" name="action" value="delete">
            <input type="hidden" name="acc_id" value="<%= acc.getAcc_id()%>">
            <input type="hidden" name="roll" value="<%= acc.getRoll()%>">
            <input type="submit" value="Delete" onclick='return delete1()'>
        </form>
    </th>
    <%
    } else {%>
    <th>
    <form action="deleteStudentinClass" method ="get">
        <input type="hidden" name="acc_id" value="<%= acc.getAcc_id()%>">
        <input type="hidden" name="class_id" value="<%= request.getAttribute("class_id")%>">
        <input type="submit" value="Delete" onclick='return delete1()'>
    </form>
</th>
<% } %>
</tr>   
<%
        }
    }
%>


</table>
</body>
</html>
